<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use think\facade\Filesystem;
use think\Request;
use think\Response;
use utils\YPUpload;

class Attach extends BaseController
{

    /**
     * 显示资源列表
     *
     * @return Response
     */
    public function index()
    {
        //
        $this->checkLogin();
        $page = $this->request->param('page', 1);
        $all = (bool)$this->request->param('all', false);
        if ($all && $this->auth->check('view_attach_list', $this->user->id)) {
            $attaches = \app\model\Attach::page($page, 50)
                ->select();
        } else {
            $attaches = \app\model\Attach::where('user_id', $this->user->id)
                ->page($page, 50)
                ->select();
        }
        return $this->success('success', $attaches);
    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request)
    {
        //
        $this->checkLogin();
        $file = $request->file('file');
        $attach = \app\model\Attach::where('key', md5($file->md5() . $this->user->card))->find();
        if (!empty($attach)) return $this->success('upload success', $attach);
        $save_name = Filesystem::putFile('attach', $file);
        $path = str_replace("\\", "/", $save_name);
        $data = [
            'key' => md5($file->md5() . $this->user->card),
            'user_id' => $this->user->id,
            'name' => $file->getOriginalName(),
            'mimeType' => $file->getOriginalMime(),
            'path' => $path
        ];
        $attach = \app\model\Attach::create($data);
        if (env('upyun_status')) {

            $access_key = 'admin';
            $secret_key = '5Xfj0zpPUPAeqQ0WKDzpzZARb9BkEO5M';
            $bucket = 'image-cdn-gxcvucnc';
            $endpoint = 'https://v0.api.upyun.com/';
            $file_path = app()->getRuntimePath() . 'storage/' . $path;
            $upload = new YPUpload($access_key, $secret_key, $bucket, $endpoint);
            $response = $upload->upload($file_path, '/app_gxcvucnc_cn/' . $path);
            if ($response->getStatusCode() !== 200) {
                return $this->error('上传失败', $response);
            }
        }
        return $this->success('upload success', $attach);
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return Response
     */
    public function read($id)
    {
        //
        $attach = \app\model\Attach::where('key', $id)->find();
        if (empty($attach)) return response('404 Not Found', 404);
        if (env('upyun_status') && env('redirect_upyun')) {
            return redirect(env('upyun_domain') . '/' . $attach->path);
        } else {
            $url = app()->getRuntimePath() . 'storage/' . $attach->path;
            //$url = str_replace("\\", "/", $url);
            if (!file_exists($url)) return response('404 Not Found', 404);
            return download($url, $attach->name);
        }

    }

    /**
     * 保存更新的资源
     *
     * @param Request $request
     * @param int $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return Response
     */
    public function delete($id)
    {
        //
    }

}
